package com.buildings.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.bean.TaskBean;
import com.bumptech.glide.Glide;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class PhotoRecycleAdapter extends RecyclerView.Adapter<PhotoRecycleAdapter.ViewHolder> {
    private List<Uri> data;
    private Activity activity;
    private OnItemClickListener listener;
    private int joninType;
    private List<String> srcHttpList;

    public PhotoRecycleAdapter(Activity activity, List<Uri> data, List<String> srcHttpList, int joninType, OnItemClickListener listener) {
        this.data = data;
        this.activity = activity;
        this.listener = listener;
        this.joninType = joninType;
        this.srcHttpList = srcHttpList;
    }

    public void update(List<Uri> data) {
        this.data = data;
        this.notifyDataSetChanged();
    }


    @Override
    public PhotoRecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_photo_recycle, parent, false);
        PhotoRecycleAdapter.ViewHolder holder = new PhotoRecycleAdapter.ViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int i) {

        if (i >= data.size()) {
            holder.ivPic.setImageResource(R.mipmap.iv_add);
            holder.ivDelete.setVisibility(View.GONE);
        } else {
            if (srcHttpList.size() > 0) {
                Glide.with(activity)
                        .load(srcHttpList.get(i))
                        .error(R.mipmap.ic_picture)
                        .into(holder.ivPic);
            } else {
                Glide.with(activity)
                        .load(data.get(i))
                        .error(R.mipmap.ic_picture)
                        .into(holder.ivPic);
            }
            if (2 == joninType){
                holder.ivDelete.setVisibility(View.GONE);
            }else {
                holder.ivDelete.setVisibility(View.VISIBLE);
            }
        }
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDelete(v, i);
                }
            }
        });
        holder.ivPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    if (i < data.size()) {
                        //查看
                    } else {
                        listener.onAddView(v, i);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (2 == joninType) {
            //详情
            return data.size();
        } else {
            //保存
            return data.size() >= 10 ? 10 : data.size() + 1;
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivDelete, ivPic;

        public ViewHolder(View itemView) {
            super(itemView);
            ivDelete = itemView.findViewById(R.id.iv_delete);
            ivPic = itemView.findViewById(R.id.iv_task_pic);

        }
    }


    public interface OnItemClickListener {
        void onQuery(View view, int position);

        void onDelete(View view, int position);

        void onAddView(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
